
package view.conponent;

import com.alibaba.fastjson.JSON;
import model.hospital.*;
import model.user.Staff;
import net.ToServer;
import net.passClass.PassBuildings;
import net.passClass.PassData;
import viewModel.CurrentUser;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Objects;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EquipmentManager extends JSplitPane {


    private final JTree tree = new JTree();

    public EquipmentManager() {

        requestData();

        JPanel treePanel = new JPanel();
        treePanel.setMaximumSize(new Dimension(220, 600));
        treePanel.setMinimumSize(new Dimension(220, 600));
        treePanel.setBackground(Color.WHITE);
        treePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        treePanel.add(tree);

        JScrollPane sp = new JScrollPane(treePanel);
        this.setLeftComponent(sp);
        this.setContinuousLayout(true);
        this.setDividerLocation(250);
        this.setDividerSize(8);

        Box vBox = Box.createVerticalBox();

        this.setRightComponent(vBox);

        Box textFieldBox = Box.createVerticalBox();
        textFieldBox.setMaximumSize(new Dimension(300, 200));
        textFieldBox.setMinimumSize(new Dimension(200, 200));
        vBox.add(textFieldBox);

        Box btnBox = Box.createVerticalBox();
        btnBox.setMaximumSize(new Dimension(300, 200));
        btnBox.setMinimumSize(new Dimension(200, 200));
        vBox.add(btnBox);

        Box box1 = Box.createHorizontalBox();
        JTextField buildingNumber = new JTextField(9);
        JLabel buildingNumberStr = new  JLabel("楼号");
        box1.add(buildingNumberStr);
        box1.add(buildingNumber);
        textFieldBox.add(box1);

        Box box2 = Box.createHorizontalBox();
        JTextField floorNumber = new JTextField(9);
        JLabel floorNumberStr = new  JLabel("楼层");
        box2.add(floorNumberStr);
        box2.add(floorNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box2);

        Box box3 = Box.createHorizontalBox();
        JTextField roomNumber = new JTextField(9);
        JLabel roomNumberStr = new  JLabel("房号");
        box3.add(roomNumberStr);
        box3.add(roomNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box3);

        Box box4 = Box.createHorizontalBox();
        JTextField time = new JTextField(9);
        JLabel timeStr = new  JLabel("签出时间");
        box4.add(timeStr);
        box4.add(time);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box4);

        JButton check_inBtn = new JButton("签入");

        btnBox.add(check_inBtn);

        check_inBtn.addActionListener(e -> {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m");
            long timeNum = 0;
            try {
                String str = time.getText();
                String pattern = "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}";

                Pattern r = Pattern.compile(pattern);
                Matcher m = r.matcher(str);
                if (m.matches()) {
                    timeNum = dateFormat.parse(str).getTime();
                }
                PassData receive = ToServer.receive("/hospital/equipment/request",
                        "buildingNumber:" + buildingNumber.getText() + "\n" +
                                "floorNumber:" + floorNumber.getText() + "\n" +
                                "roomNumber:" + roomNumber.getText() + "\n" +
                                "check_out:" + timeNum + "\n" +
                                "userUID:"+ ((Staff)CurrentUser.user).getUid());
                if(!receive.isFlag()){
                    JOptionPane.showMessageDialog(check_inBtn, receive.getMessage());
                }
            } catch (ParseException parseException) {
                JOptionPane.showMessageDialog(check_inBtn, "信息格式错误");
            }
        });

        tree.addTreeSelectionListener(e -> {
            Object[] path = Objects.requireNonNull(tree.getSelectionPath()).getPath();
            if(path.length>1){
                buildingNumber.setText(String.valueOf(path[1]).substring(2));
                if(path.length>2){
                    floorNumber.setText(String.valueOf(path[2]).substring(2));
                    if(path.length>3){
                        roomNumber.setText(String.valueOf(path[3]).substring(2));
                    }
                }
            }
        });

    }

    public void requestData(TreeSet<Building> buildings){

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("东软颐养社区中心"){
            {
                DefaultMutableTreeNode building;
                DefaultMutableTreeNode floor;
                DefaultMutableTreeNode room;

                for(Building aBuilding : buildings){
                    building = new DefaultMutableTreeNode("建筑"+aBuilding.getBuildingNumber());
                    for(Floor aFloor : aBuilding.getFloors()){
                        floor = new DefaultMutableTreeNode("楼层"+aFloor.getFloorNumber());
                        for(Room aRoom : aFloor.getRooms()){
                            room = new DefaultMutableTreeNode("房间"+aRoom.getLocation().getRoomNumber());
                            if(aRoom.getEquipment()!=null){
                                room.add(new DefaultMutableTreeNode("设备"+aRoom.getType()));
                            }
                            floor.add(room);
                        }
                        building.add(floor);
                    }
                    add(building);
                }

            }
        };
        tree.setModel(new DefaultTreeModel(root));
    }

    public void requestData(){

        PassBuildings data = JSON.parseObject(JSON.toJSONString(
                ToServer.receive("/hospital/get","\n")),PassBuildings.class);
        TreeSet<Building> buildings = data.getData();
        requestData(buildings);
    }
}